home *** CD-ROM | disk | FTP | other *** search
- /*
- * rpcmisc Miscellaneous functions for RPC startup and shutdown.
- * This code is partially snarfed from rpcgen -s tcp -s udp,
- * partly written by Mark Shand, Donald Becker, and Rick
- * Sladkey. It was tweaked slightly by Olaf Kirch to be
- * useable by both nfsd and mountd.
- *
- * This software may be used for any purpose provided
- * the above copyright notice is retained. It is supplied
- * as is, with no warranty expressed or implied.
- */
-
- #include "system.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <rpc/pmap_clnt.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <memory.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include "rpcmisc.h"
- #include "logging.h"
-
- static _PRO (void closedown, (int sig) );
- static _PRO (int makesock, (int port, int proto, int socksz) );
-
- #define _RPCSVC_CLOSEDOWN 120
- int _rpcpmstart = 0;
- int _rpcfdtype = 0;
- int _rpcsvcdirty = 0;
-
- void
- rpc_init(name, prog, vers, dispatch, defport, bufsiz)
- char *name;
- int prog;
- int vers;
- void (*dispatch)();
- int defport;
- int bufsiz;
- {
- struct sockaddr_in saddr;
- SVCXPRT *transp;
- int sock;
- int asize;
-
- asize = sizeof(saddr);
- sock = 0;
- _rpcfdtype = 0;
- if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
- int ssize = sizeof (int);
- if (saddr.sin_family != AF_INET)
- exit(1);
- if (getsockopt(0, SOL_SOCKET, SO_TYPE,
- (char *)&_rpcfdtype, &ssize) == -1)
- exit(1);
- _rpcpmstart = 1;
- } else {
- pmap_unset(prog, vers);
- sock = RPC_ANYSOCK;
- }
-
- if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
- if (_rpcfdtype == 0 && defport != 0 &&
- ((sock = makesock(defport, IPPROTO_UDP, bufsiz)) < 0)) {
- fprintf(stderr, "%s: could not make a UDP socket\n",
- name);
- exit(1);
- }
- transp = svcudp_create(sock);
- if (transp == NULL) {
- fprintf(stderr, "cannot create udp service.");
- exit(1);
- }
- if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
- fprintf(stderr, "unable to register (%s, %d, udp).",
- name, vers);
- exit(1);
- }
- }
-
- if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
- if (_rpcfdtype == 0 && defport != 0 &&
- ((sock = makesock(defport, IPPROTO_TCP, bufsiz)) < 0)) {
- fprintf(stderr, "%s: could not make a TCP socket\n",
- name);
- exit(1);
- }
- transp = svctcp_create(sock, 0, 0);
- if (transp == NULL) {
- fprintf(stderr, "cannot create tcp service.");
- exit(1);
- }
- if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
- fprintf(stderr, "unable to register (%s, %d, tcp).",
- name, vers);
- exit(1);
- }
- }
-
- if (_rpcpmstart) {
- signal (SIGALRM, closedown);
- alarm (_RPCSVC_CLOSEDOWN);
- }
-
- /* We ignore SIGPIPE. This is a somewhat dubious method to help
- * an obscure problem with mountd or nfsd dying mysteriously
- * after receiving a SIGHUP. This hopefully makes the problem
- * go away, although I have no idea what causes it.
- */
- {
- struct sigaction pipeact = { SIG_IGN, 0, 0, NULL };
-
- sigaction(SIGPIPE, &pipeact, NULL);
- }
- }
-
- static void closedown(sig)
- int sig;
- {
- (void) signal(sig, closedown);
- if (_rpcsvcdirty == 0) {
- extern fd_set svc_fdset;
- static int size;
- int i, openfd;
-
- if (_rpcfdtype == SOCK_DGRAM)
- exit(0);
- if (size == 0) {
- size = getdtablesize();
- }
- for (i = 0, openfd = 0; i < size && openfd < 2; i++)
- if (FD_ISSET(i, &svc_fdset))
- openfd++;
- if (openfd <= 1)
- exit(0);
- }
- (void) alarm(_RPCSVC_CLOSEDOWN);
- }
-
- static int makesock(port, proto, socksz)
- int port;
- int proto;
- int socksz;
- {
- struct sockaddr_in sin;
- int s;
- int sock_type;
- int val;
-
- sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
- s = socket(AF_INET, sock_type, proto);
- if (s < 0) {
- dprintf(L_ERROR, "Could not make a socket: %s\n",
- strerror(errno));
- return (-1);
- }
- memset((char *) &sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = INADDR_ANY;
- sin.sin_port = htons(port);
-
- #ifdef DEBUG
- val = 1;
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
- dprintf(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
- #endif
-
- #ifdef SO_SNDBUF
- {
- int sblen, rblen;
-
- /* 1024 for rpc & transport overheads */
- sblen = rblen = socksz + 1024;
- if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
- setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
- dprintf(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
- }
- #endif /* SO_SNDBUF */
-
- if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
- dprintf(L_ERROR, "Could not bind name to socket: %s\n",
- strerror(errno));
- return (-1);
- }
- return (s);
- }
-
-